﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Reflection;
namespace BookStore
{
    public partial class Frmgl : Form
    {
        private String _banner;
        public Frmgl(String username)
        {
            _banner = username;
            InitializeComponent();
        }
 
        //加载书籍
        private void PushDataToDataGridView()
        {
            sqlConnection sqlConnection = new sqlConnection();
            SqlConnection Connection = sqlConnection.CreateSqlConnection();
            //如果有过滤条件则显示符合条件的书籍，否则显示全部书籍
            if (CFilter.Text == "无" || CFilter.SelectedIndex == -1)
            {
                sqlDataAdapter sqlDataAdapter = new sqlDataAdapter();
                string SelectCommand = "Select * from BookStore.dbo.bookinfo";
                sqlDataAdapter.LoadDataToDataGridView(SelectCommand, BookdataGridView,Connection);
            }
            else
            {
                sqlDataAdapter sqlDataAdapter = new sqlDataAdapter();
                string SelectCommand = "Select * from BookStore.dbo.bookinfo where class = @CFilter";
                SqlParameter fclass = new SqlParameter("@CFilter", SqlDbType.NVarChar, 10) { Value = CFilter.Text };
                sqlDataAdapter.LoadDataToDataGridView(SelectCommand, fclass, BookdataGridView,Connection);
            }
            return;
        }
        //清除sql连接并退出系统
        private void LExit_Click(object sender, EventArgs e)
        {
            //_SqlConnectionManager.Dispose();
            Exit.FrmClose();
        }

        //录入书籍信息的功能
        private void BC_Click(object sender, EventArgs e)
        {
            //处理输入的数据
            int price,quantity;
            TBookName.Text = TBookName.Text.Trim();
            TAuthor.Text = TAuthor.Text.Trim();
            TPrice.Text = TPrice.Text.Trim();
            price = Convert.ToInt32(TPrice.Text);
            TQuantity.Text = TQuantity.Text.Trim();
            TDescription.Text = TDescription.Text.Trim();
            if (TQuantity.Text != string.Empty)
            { 
                quantity = Convert.ToInt32(TQuantity.Text); 
            }
            //检查书名是否为空
            if (Log.CheckBookname(TBookName.Text) == false)
            {
                return;
            }
            else
            {

                //如果查询到相同的书名就提示，否则记录书本信息
            /*   if (sqlHelper.ExecuteScalar(sqlSelectCommand, Connection , sqlParameters[0]) != null)
               {
                    MessageBox.Show("already got this one please check book name!","Alert",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    int rowsaffected =  sqlHelper.ExecuteNonQuery(sqlInsertCommand, Connection , sqlParameters);
                    if (rowsaffected == 0) 
                    {
                        MessageBox.Show("no insert!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    TextboxClear();
                    PushDataToDataGridView();
                }*/
             }
            return;  
        }

        private void TAuthor_Click(object sender, EventArgs e)
        {
            if(TAuthor.Text == "请于此处输入作者姓名")
                TAuthor.Text = "";
            return;
        }


        private void TBookName_Click(object sender, EventArgs e)
        {
            if(TBookName.Text == "请于此处输入书籍名称")
                TBookName.Text = "";
            return;
        }

        //banner显示当前登录用户的用户名
        private void Frmgl_Load(object sender, EventArgs e)
        {
            LBanner.Text = "Hello " + _banner + "!";
            return;
        }

        //此事件为筛选datagridview控件中的书籍
        private void CFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            //如果没有筛选条件就显示全部书籍记录
            if (CFilter.Text == "无")
            {
                PushDataToDataGridView();
            }
            else
            {
               // SqlConnection Connection = _SqlConnectionManager.CreateSqlConnection();
                sqlDataAdapter sqlDataAdapter = new sqlDataAdapter();
                string SelectCommand = "Select * from BookStore.dbo.bookinfo where class = @CFilter";
                SqlParameter fclass = new SqlParameter("@CFilter", SqlDbType.NVarChar, 10) { Value = CFilter.Text };
               // sqlDataAdapter.LoadDataToDataGridView(SelectCommand, fclass, BookdataGridView, Connection);
            }
            return;
            
        }

        //清空文本输入框内容
        private void TextboxClear()
        {
            TBookName.Text = string.Empty;
            TAuthor.Text = string.Empty;
            CClass.Text = string.Empty;
            TPrice.Text = string.Empty;
            TQuantity.Text = string.Empty;
            TDescription.Text = string.Empty;
            return;
        }

        //当选中某行书籍记录时，对应文本框的内容会变更为数据库的数据
        private void BookdataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            TBookName.Text = BookdataGridView.SelectedRows[0].Cells[0].Value.ToString();
            TAuthor.Text = BookdataGridView.SelectedRows[0].Cells[1].Value.ToString();
            CClass.Text = BookdataGridView.SelectedRows[0].Cells[2].Value.ToString();
            TPrice.Text = BookdataGridView.SelectedRows[0].Cells[4].Value.ToString();
            TDescription.Text = BookdataGridView.SelectedRows[0].Cells[3].Value.ToString();
            return;
        }

        //删除某个书籍记录
        private void BD_Click(object sender, EventArgs e)
        {
            TBookName.Text = TBookName.Text.Trim();
            string DeleteSqlCommand = "Delete from BookStore.dbo.bookinfo where bookname = @Bookname";
            SqlParameter SqlParameterBookname = new SqlParameter("@Bookname", SqlDbType.NVarChar, 20) { Value = TBookName.Text };
            //如果TBookName内容为空则弹窗并返回，否则删除书籍记录
            if(Log.CheckBookname(TBookName.Text) == false)
            {
                MessageBox.Show("Bookname is require!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                //SqlConnection Connection = _SqlConnectionManager.CreateSqlConnection();
                sqlHelper sqlHelper = new sqlHelper();


               // int a = sqlHelper.ExecuteNonQuery(DeleteSqlCommand, Connection ,SqlParameterBookname);
                //如果删除记录失败则弹窗提示
                if (true) 
                {
                    MessageBox.Show("delete failed", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                //清空各个文本框
                TextboxClear();
                //显示最新的数据库记录
            }
            return;
        }

        private void BU_Click(object sender, EventArgs e)
        {
            int price, quantity;
            TBookName.Text = TBookName.Text.Trim();
            TAuthor.Text = TAuthor.Text.Trim();
            TPrice.Text = TPrice.Text.Trim();
            price = Convert.ToInt32(TPrice.Text);
            TQuantity.Text = TQuantity.Text.Trim();
            TDescription.Text = TDescription.Text.Trim();
            if (TQuantity.Text != string.Empty)
            {
                quantity = Convert.ToInt32(TQuantity.Text);
            }
            if (Log.CheckBookname(TBookName.Text)==false)
            {
                MessageBox.Show("If you want to update a book record,you must input a bookname to locate record", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                string UpdateSqlCommand = "Update BookStore.dbo.bookinfo set bookname = @bookname , author = @author , class = @class , description = @description , price = @price where bookname = @bookname";
                //SqlConnection Connection = _SqlConnectionManager.CreateSqlConnection();
                sqlHelper sqlHelper = new sqlHelper();
                SqlParameter[] sqlParameters = {
                                                    new SqlParameter("@bookname",SqlDbType.NVarChar,20) {  Value = TBookName.Text },
                                                    new SqlParameter("@author",SqlDbType.NVarChar,10) {  Value = TAuthor.Text },
                                                    new SqlParameter("@class",SqlDbType.NVarChar,10) {  Value = CClass.Text },
                                                    new SqlParameter("@description",SqlDbType.NVarChar,50) { Value = TDescription.Text },
                                                    new SqlParameter("@price",SqlDbType.Int) { Value = price },
                                                };
               /* if (sqlHelper.ExecuteNonQuery(UpdateSqlCommand,Connection, sqlParameters) == 0) 
                {
                    MessageBox.Show("Update failed", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }*/
                //清空各个文本框
                TextboxClear();
                //显示最新的数据库记录
                PushDataToDataGridView();
                return;
            }
            
        }
    }
}
